<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="联系人姓名" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱地址" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入职位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属企业" prop="enterpriseId">
        <el-input
          v-model="queryParams.enterpriseId"
          placeholder="请输入所属企业"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="所属部门名称" prop="departmentName">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入所属部门名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:contacts:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:contacts:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:contacts:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:contacts:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contactsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账号ID" align="center" prop="accountId" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="邮箱地址" align="center" prop="email" />
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="职位" align="center" prop="position" />
      <el-table-column label="所属企业" align="center" prop="enterpriseId" />
      <el-table-column label="所属部门" align="center" prop="departmentName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:contacts:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:contacts:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业联系人对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="contactsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择用户">
          <el-select v-model="selUser"  placeholder="请选择">
            <el-option
                v-for="item in userList"
                :key="item.userName"
                :label="item.nickName+','+item.userName"
                :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="联系人姓名" prop="contactPerson">-->
<!--          <el-input v-model="form.contactPerson" placeholder="请输入联系人姓名" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="邮箱地址" prop="email">-->
<!--          <el-input v-model="form.email" placeholder="请输入邮箱地址" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="手机号" prop="phoneNumber">-->
<!--          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="职位" prop="position">-->
<!--          <el-input v-model="form.position" placeholder="请输入职位" />-->
<!--        </el-form-item>-->
        <el-form-item label="所属企业" >
          <el-select v-model="selEnter"  placeholder="请选择企业">
            <el-option
                v-for="ei in enterList"
                :key="ei.id"
                :label="ei.name"
                :value="ei"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入所属部门名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Contacts">
import { listContacts, getContacts, delContacts, addContacts, updateContacts } from "@/api/business/contacts";
import { listUser } from "@/api/system/user";
import { listEnterprises } from "@/api/business/enterprises";
const { proxy } = getCurrentInstance();

const contactsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const userList = ref([]);
const enterList = ref([]);
const title = ref("");
const selUser=ref({});
const selEnter=ref({});
const data = reactive({
  form: {departmentName:null,accountId: null},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    contactPerson: null,
    email: null,
    phoneNumber: null,
    position: null,
    enterpriseId: null,
    departmentName: null
  },
  listParams:{
    pageNum: 1,
    pageSize: 100
  },
  rules: {
    contactPerson: [
      { required: true, message: "联系人姓名不能为空", trigger: "blur" }
    ],
    email: [
      { required: true, message: "邮箱地址不能为空", trigger: "blur" }
    ],
    enterpriseId: [
      { required: true, message: "所属企业不能为空", trigger: "blur" }
    ],
  }
});

const { listParams,queryParams, form, rules } = toRefs(data);

/** 查询企业联系人列表 */
function getList() {
  loading.value = true;
  listContacts(queryParams.value).then(response => {
    contactsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getUsers() {

  listUser(listParams.value).then(res => {

    userList.value = res.rows;
  });
};
function getEnters() {
  listEnterprises(listParams.value).then(res => {
    enterList.value = res.rows;
  });
};
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    accountId: null,
    contactPerson: null,
    email: null,
    phoneNumber: null,
    position: null,
    enterpriseId: null,
    departmentName: null
  };
  proxy.resetForm("contactsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.accountId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加企业联系人";
  getUsers();
  getEnters();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _accountId = row.accountId || ids.value
  getContacts(_accountId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改企业联系人";
  });
}
function fillUser_Enter(){
  console.info(selEnter.value.enterpriseId);
  console.info(selUser.value);
  form.value={...form.value,contactUser: selUser.value.userName,
    contactPerson : selUser.value.nickName,
    email:selUser.value.email,
    phoneNumber:selUser.value.phoneNumber,

    enterpriseId:selEnter.value.enterpriseId,
    position:selUser.value.position
  };
  console.info(form.value);
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["contactsRef"].validate(valid => {
    if (valid) {
      fillUser_Enter();
      if (form.value.accountId != null) {
        updateContacts(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addContacts(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _accountIds = row.accountId || ids.value;
  proxy.$modal.confirm('是否确认删除企业联系人编号为"' + _accountIds + '"的数据项？').then(function() {
    return delContacts(_accountIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/contacts/export', {
    ...queryParams.value
  }, `contacts_${new Date().getTime()}.xlsx`)
}

getList();
</script>
